package com.teame.readbaby.util;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListAdapter;
import android.widget.ListView;
/**
 * 公共组件，包装了一些显示对话框的方法
 *
 * @author dupengtao
 */
public class CommonUtil {

	public static void showInfoDialog(Context context, String message) {
		showInfoDialog(context, message, "标题ʾ", "确定", null);
	}

	public static void showInfoDialog(Context context, String message, String titleStr, String positiveStr,
			DialogInterface.OnClickListener onClickListener) {
		AlertDialog.Builder localBuilder = new AlertDialog.Builder(context);
		localBuilder.setTitle(titleStr);
		localBuilder.setMessage(message);
		if (onClickListener == null)
			onClickListener = new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					dialog.dismiss();
				}
			};
		localBuilder.setPositiveButton(positiveStr, onClickListener);
		localBuilder.show();
	}

	/**
	 * ��null ֵ��Ϊ�հ��ַ�
	 * 
	 * @param paramString
	 * @return
	 */
	public static String stringFilterNull(String paramString) {
		if (paramString == null)
			paramString = "";
		return paramString;
	}

	public static void setListViewHeightBasedOnChildren(ListView paramListView) {
		// �õ�ListView ��������
		ListAdapter localListAdapter = paramListView.getAdapter();
		if (localListAdapter == null)
			return;
		int i = 0;

		// �õ�adapter�е�items������
		int k = localListAdapter.getCount();
		for (int j = 0; j <= k; j++) {
			if (j >= k) {
				// ��ò��ֵĲ���
				ViewGroup.LayoutParams localLayoutParams = paramListView.getLayoutParams();
				int m = paramListView.getDividerHeight();
				int n = localListAdapter.getCount();
				int i1 = m * n + i;
				localLayoutParams.height = i1;
				paramListView.setLayoutParams(localLayoutParams);
				return;
			}
			View localView = localListAdapter.getView(j, null, paramListView);
			localView.measure(0, 0);
			int i2 = localView.getMeasuredHeight();
			i += i2;

		}
	}

	public static void configListView(ListView paramListView) {
		// �õ�ListView ��������
		ListAdapter localListAdapter = paramListView.getAdapter();
		if (localListAdapter == null)
			return;
		int height = 0;

		// �õ�adapter�е�items������
		int k = localListAdapter.getCount();
		for (int j = 0; j < k; j++) {

			View localView = localListAdapter.getView(j, null, paramListView);

			localView.measure(30, 50);

			int i2 = localView.getMeasuredHeight();
			height += i2;

		}

		ViewGroup.LayoutParams localLayoutParams = paramListView.getLayoutParams();
		int m = paramListView.getDividerHeight();
		int i1 = m * (k - 1) + height;
		localLayoutParams.height = i1;
		paramListView.setLayoutParams(localLayoutParams);
	}
}
